package com.xu.t1.mapper;

import com.xu.t1.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 负责用户表数据的持久化接口类
 *
 * @author xu
 *
 */

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM users WHERE username LIKE CONCAT('%', #{keyword}, '%') LIMIT 50")
    List<User> findUsersByKeyword(@Param("keyword") String keyword);

    @Update("UPDATE users SET record_count = record_count + #{delta}, version = version + 1 WHERE id = #{userId} AND version = #{version}")
    int increaseRecordCount(@Param("userId") Long userId,
                            @Param("delta") Integer delta,
                            @Param("version") Integer version);
    // 查询当前的version的值
    @Select("SELECT record_count, version FROM users WHERE id = #{userId}")
    User findUserById(@Param("userId") Long userId);


}

